﻿using LotusIslandOrderProcessing.PLCMake;
using Newtonsoft.Json;
using System;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;

namespace LotusIslandOrderProcessing.model
{
    public class OrderRequestDeleteOrOut
    {
        private static OrderRequestDeleteOrOut instance;
        private OrderRequestDeleteOrOut() { }
        public static OrderRequestDeleteOrOut Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new OrderRequestDeleteOrOut();
                }
                return instance;
            }
        }
        public byte[] GetUrlToBase(string url)
        {
#pragma warning disable CS0168 // 声明了变量，但从未使用过
            try
            {
                var base64String = "";
                HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
                string base64sstr = string.Empty;
                using (Stream stream = webResponse.GetResponseStream())
                {
                    var mStream = new MemoryStream();

                    byte[] buffer = new byte[1024];
                    int byteCount;
                    do
                    {
                        byteCount = stream.Read(buffer, 0, buffer.Length);
                        mStream.Write(buffer, 0, byteCount);
                    } while (byteCount > 0);

                    mStream.Seek(0, SeekOrigin.Begin);
                    base64String = Convert.ToBase64String(mStream.ToArray());
                }
                //return base64String;
                WebClient mywebclient = new WebClient();
                byte[] Bytes = mywebclient.DownloadData(url);
                //string Base64string = Convert.ToBase64String(Bytes);
                return Bytes;
            }
            catch (Exception e)
            {
                return new byte[0];
            }
#pragma warning restore CS0168 // 声明了变量，但从未使用过
        }
        /// <summary>
        /// 打印图片保存
        /// </summary>
        /// <param name="ImageData"></param>
        /// <param name="imageName"></param>
        private void SaveImage(byte[] ImageData, string imageName)
        {
            try
            {
                //保存图片到本地文件夹
                System.IO.MemoryStream ms = new System.IO.MemoryStream(ImageData);
                System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                //保存到磁盘文件
                string imagePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"打印";
                if (!System.IO.Directory.Exists(imagePath))
                    System.IO.Directory.CreateDirectory(imagePath);
                img.Save(System.IO.Path.Combine(imagePath, imageName), System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Dispose();
            }
            catch (Exception exception)
            { };
        }
        /// <summary>
        /// 打印
        /// </summary>
        public void DYShow()
        {
            while (true)
            {
                try
                {
                    if (PlcReadMsg.Instance.Print3 == 1 && MainWindow.makeModel1s.Any(a => a.Guid == PlcReadMsg.Instance.Order_Number3 && a.Type < 2))
                    {
                        var model = MainWindow.makeModel1s.FirstOrDefault(a => a.Guid == PlcReadMsg.Instance.Order_Number3);
                        if (model != null)
                        {
                            dy1(3, model.LHImg);
                            model.Type = 2;
                        }
                    }
                    else if (PlcReadMsg.Instance.Print4 == 1 && MainWindow.makeModel1s.Any(a => a.Guid == PlcReadMsg.Instance.Order_Number4 && a.Type < 2))
                    {
                        var model = MainWindow.makeModel1s.FirstOrDefault(a => a.Guid == PlcReadMsg.Instance.Order_Number4);
                        if (model != null)
                        {
                            dy1(4, model.LHImg);
                            model.Type = 2;
                        }
                    }
                }
                catch (Exception)
                {
                }
                Thread.Sleep(500);
            }
        }

        public void dy1(int classcode, string img)
        {
            try
            {
                var imgs = GetUrlToBase(img);
                SaveImage(imgs, "打印图片.jpg");
                SaveImg();
                PrinterWebSocket.Instance.DyCS1(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"打印\打印图片.png");
                OrderRequst.Instance.OrderPrint(classcode, 1);
            }
            catch (Exception)
            {
            }
        }
        public void SaveImg()
        {
            try
            {
                using (Image originalImage = Image.FromFile(@"打印/打印图片.jpg"))
                {
                    // 确定圆形的尺寸和位置
                    int width = 200; // 圆形宽度
                    int height = 200; // 圆形高度
                    int x = (originalImage.Width - width) / 2; // 圆形中心在图片中的x坐标
                    int y = (originalImage.Height - height) / 2; // 圆形中心在图片中的y坐标

                    // 创建一个新的Bitmap用于绘制圆形图像
                    using (Bitmap bmp = new Bitmap(width, height))
                    {
                        using (Graphics g = Graphics.FromImage(bmp))
                        {
                            // 设置高质量插值法
                            g.SmoothingMode = SmoothingMode.HighQuality;
                            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            g.CompositingQuality = CompositingQuality.HighQuality;
                            g.Clear(Color.White);
                            // 创建一个路径用于圆形
                            using (GraphicsPath path = new GraphicsPath())
                            {
                                path.AddEllipse(0, 0, width, height);
                                g.SetClip(path);

                                // 绘制图片到圆形路径上
                                g.DrawImage(originalImage, new Rectangle(0, 0, width, height));
                            }
                        }

                        // 保存或显示新的圆形图像
                        bmp.Save(@"打印/打印图片.png", ImageFormat.Png);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// 出餐
        /// </summary>
        public void CCShow()
        {
            while (true)
            {
                try
                {
                    if (PlcReadMsg.Instance.Making3 == 2)
                    {
                        var model = MainWindow.makeModel1s.FirstOrDefault(a => a.Guid == PlcReadMsg.Instance.Order_Number3);
                        if (model != null)
                        {
                            SpeechVoice.Instance.VoicePlayback(model.OrderNum + " 号请取餐");
                            Thread.Sleep(5000);
                        }
                    }
                    if (PlcReadMsg.Instance.Making4 == 2)
                    {
                        var model = MainWindow.makeModel1s.FirstOrDefault(a => a.Guid == PlcReadMsg.Instance.Order_Number4);
                        if (model != null)
                        {
                            SpeechVoice.Instance.VoicePlayback(model.OrderNum + " 号请取餐");
                            Thread.Sleep(5000);
                        }
                    }
                }
                catch (Exception)
                {
                }
                Thread.Sleep(2000);
            }
        }
        /// <summary>
        /// 删除订单
        /// </summary>
        public void OrderDelete()
        {
            while (true)
            {
                try
                {
                    if (PlcReadMsg.Instance.Making3 == 0 && MainWindow.makeModel1s.Any(a => a.OrderMaking == 1 && a.Type > 0))
                    {
                        var model = MainWindow.makeModel1s.FirstOrDefault(a => a.OrderMaking == 1);
                        while (true)
                        {
                            try
                            {

                                EditDeviceOrderStatus editDeviceOrder1 = new EditDeviceOrderStatus();
                                editDeviceOrder1.status = 5;
                                editDeviceOrder1.orderId = model.OrderId;
                                editDeviceOrder1.orderGoodsId = model.OrderGoodsId;
                                string lit = JsonConvert.SerializeObject(editDeviceOrder1);
                                var ddd1 = HttpMsg.Instance.editDeviceOrderStatusMsg(lit);
                                if (ddd1 != null)
                                {
                                    if (ddd1 != "")
                                    {
                                        var list10 = JsonConvert.DeserializeObject<ListMsg>(ddd1);
                                        if (list10 != null)
                                        {
                                            if (list10.code == "0" || list10.code == "1")
                                            {
                                                Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                                break;
                                            }
                                        }
                                        Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                    }
                                }
                            }
                            catch (Exception)
                            {
                            }
                            Thread.Sleep(100);
                        }
                        MainWindow.makeModel1s.Remove(MainWindow.makeModel1s.FirstOrDefault(a => a.OrderMaking == 1));
                    }
                    if (PlcReadMsg.Instance.Making4 == 0 && MainWindow.makeModel1s.Any(a => a.OrderMaking == 2 && a.Type > 0))
                    {
                        var model = MainWindow.makeModel1s.FirstOrDefault(a => a.OrderMaking == 2);
                        while (true)
                        {
                            try
                            {

                                EditDeviceOrderStatus editDeviceOrder1 = new EditDeviceOrderStatus();
                                editDeviceOrder1.status = 5;
                                editDeviceOrder1.orderId = model.OrderId;
                                editDeviceOrder1.orderGoodsId = model.OrderGoodsId;
                                string lit = JsonConvert.SerializeObject(editDeviceOrder1);
                                var ddd1 = HttpMsg.Instance.editDeviceOrderStatusMsg(lit);
                                if (ddd1 != null)
                                {
                                    if (ddd1 != "")
                                    {
                                        var list10 = JsonConvert.DeserializeObject<ListMsg>(ddd1);
                                        if (list10 != null)
                                        {
                                            if (list10.code == "0" || list10.code == "1")
                                            {
                                                Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                                break;
                                            }
                                        }
                                        Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                    }
                                }
                            }
                            catch (Exception)
                            {
                            }
                            Thread.Sleep(100);
                        }
                        MainWindow.makeModel1s.Remove(MainWindow.makeModel1s.FirstOrDefault(a => a.OrderMaking == 2));
                    }
                }
                catch (Exception)
                {
                }
                Thread.Sleep(2000);
            }
        }
    }
}
